package cn.itcast.controller;

import cn.itcast.common.R;
import cn.itcast.dto.DishDto;
import cn.itcast.entity.Category;
import cn.itcast.entity.Dish;
import cn.itcast.entity.Page;
import cn.itcast.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author GuoMingXi @Email guomingxi2018@163.com
 * @Create 2022/6/1 15:36
 * @Version V1.0
 */
@RestController
@RequestMapping("/dish")
public class DishController {
  @Autowired
  private IDishService dishService;

  @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
      dishService.save(dishDto);
      return R.success("添加成功");
  }
  @GetMapping("/page")
  public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
    Page<DishDto> pageInfo = dishService.page(page, pageSize, name);
    return R.success(pageInfo);
  }
  @GetMapping("/{id}")
  public  R<DishDto> findById(@PathVariable Long id){
     return R.success(dishService.findById(id));
  }

  @PutMapping
  public R<String> update(@RequestBody DishDto dishDto){
     dishService.update(dishDto);
    return R.success("修改成功");
  }

  @GetMapping("list")
  public R<List<Dish>>  list(Long categoryId){
    List<Dish> dishes = dishService.list(categoryId);
    return R.success(dishes);
  }

  @GetMapping("listFlavor")
  public R<List<DishDto>> listFlvaor(Long categoryId,Integer status){

      List<DishDto> list = dishService.listFlavor(categoryId,status);
      return R.success(list);
  }


}
